<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      left-arrow
      @click-left="$router.back()"
      title="编辑资料"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <input type="file" ref="fileRef" hidden @change="onFileChange" />
    <!-- 个人信息 -->
    <van-cell title="头像" is-link @click="$refs.fileRef.click()">
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>

    <van-cell
      @click="isShowUpdateName = true"
      title="昵称"
      :value="user.name"
      is-link
    />

    <van-cell
      title="性别"
      @click="isShowUpdateGender = true"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
    />

    <van-cell
      title="生日"
      @click="isShowUpdateBirthdy = true"
      :value="user.birthday"
      is-link
    />
    <!-- 个人信息 -->

    <!-- 修改昵称 -->
    <van-popup
      v-model="isShowUpdateName"
      position="bottom"
      style="height: 100%"
    >
      <UpdateName
        v-if="isShowUpdateName"
        @close="isShowUpdateName = false"
        v-model="user.name"
      ></UpdateName>
    </van-popup>

    <!-- 修改性别 -->
    <van-popup v-model="isShowUpdateGender" position="bottom">
      <UpdateGender
        @close="isShowUpdateGender = false"
        v-model="user.gender"
      ></UpdateGender>
    </van-popup>
    <!-- 修改性别 -->

    <!-- 编辑生日弹层 -->
    <van-popup
      v-if="isShowUpdateBirthdy"
      v-model="isShowUpdateBirthdy"
      position="bottom"
    >
      <UpdateBirthdy
        @close="isShowUpdateBirthdy = false"
        v-model="user.birthday"
      ></UpdateBirthdy>
    </van-popup>
    <!-- 编辑生日弹层 -->

    <!-- 编辑头像 -->
    <van-popup
    v-if="isShowUpdatePhoto"
      v-model="isShowUpdatePhoto"
      position="bottom"
      :style="{height:'100%'}"
    >
      <UpdatePhoto @update-photo="user.photo=$event" :img="img" @close="isShowUpdatePhoto=false"></UpdatePhoto>
    </van-popup>
    <!-- 编辑头像 -->

  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import UpdateName from './compoents/update-name'
import UpdateGender from './compoents/update-gender'
import UpdateBirthdy from './compoents/update-birthby'
import UpdatePhoto from './compoents/update-photo'
export default {
  name: 'UserProfile',
  props: [],
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthdy,
    UpdatePhoto
  },
  data () {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthdy: false,
      isShowUpdatePhoto: false,
      img: null
    }
  },
  async created () {
    const { data: { data } } = await getUserProfile()
    this.user = data
  },
  mounted () {},
  methods: {
    onFileChange (e) {
      this.img = URL.createObjectURL(e.target.files[0])
      this.isShowUpdatePhoto = true
      this.$refs.fileRef.value = ''
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
.avatar {
  width: 60px;
  height: 60px;
}
.van-popup {
  background-color: #f5f7f9;
}
</style>
